#include <iostream>
using namespace std;
//第一个C++程序，完成于2021-08-27 by BlackJ. :)
int main()
{
    cout << "************************************************" << endl;

    cout << "本程序可实现1~n正整数的累加，按ctrl+c退出。" << endl;

    cout << "************************************************" << endl;
    
    while (true)
    {
        int sum = 0;
        int n = 0;
        int i = 1;
        cout << "------------------------------------------------" << endl;
        cout << "请输入n的值：" << endl;

        cin >> n;

        if (n < 0)
        {

            cout << "请重新输入大于0的整数" << endl;

            continue;
        }
        if (cin.fail())
        {

            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            cout << "非法字符，请重新输入" << endl;
            continue;
        }

        for (i = 1; i <= n; i++)
        {
            sum += i;
        }
        cout << "1~n的总和为：" << sum << endl; /* code */
    }

    return 0;
}